<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs2.model.*, bbs2.dao.*, bbs2.service.*, bbs2.util.*" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="../_header.jsp" %>  
<style>
    form > div { margin-top: 20px; }
</style>
</head>
<%
    request.setCharacterEncoding("UTF-8");
    String loginID = request.getParameter("loginID");
    String errMsg = null;
    
    if (loginID == null) loginID = "";
    
    if (request.getMethod().equals("POST")) {
        User user = UserDAO.selectByLoginID(loginID);
        if (user != null) {
            String passwd = request.getParameter("passwd");
            String encryptedPasswd = UserService.encryptPasswd(passwd);
            if (user.getPasswd().equals(encryptedPasswd)) {
                if (user.isEnabled()) {
                    UserService.login(session, user);
                    String returnUrl = (String)session.getAttribute("returnUrl");
                    if (returnUrl == null) returnUrl = "/bbs2";
                    response.sendRedirect(returnUrl);
                    return;
                }
                else errMsg = "비활성화된 사용자입니다";
            }
            else errMsg = "비밀번호가 잘못되었습니다";
        }
        else errMsg = "로그인 아이디가 잘못되었습니다";
    }
%>
<body>
<%@ include file="../_menu.jsp" %>
  
<div class="container">

    <h1>로그인</h1>
    <hr />
    
    <form method="post">
        <div>아이디</div>
        <input type="text" name="loginID" value="<%= loginID %>" />
    
        <div>비밀번호</div>
        <input type="password" name="passwd" />
    
        <div>
            <button type="submit" class="btn btn-primary">
                <i class="icon-ok icon-white"></i>
                로그인
            </button>
            <a class="btn" href="reset.jsp"><i class="icon-search"></i> 비밀번호 찾기</a>
            <a class="btn" href="register.jsp"><i class="icon-user"></i> 회원가입</a>
        </div>
    </form>
    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>

</div>

<%@ include file="../_footer.jsp" %>  
</body>
</html>
